#################################################################################
#                                                                               #
# Copyright (c) 2007 Luca Bruno                                                 #
#                                                                               #
# This file is part of Smalltalk YX.                                            #
#                                                                               #
# Permission is hereby granted, free of charge, to any person obtaining a copy  #
# of this software and associated documentation files (the "Software"), to deal #
# in the Software without restriction, including without limitation the rights  #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell     #
# copies of the Software, and to permit persons to whom the Software is         #
# furnished to do so, subject to the following conditions:                      #
#                                                                               #
# The above copyright notice and this permission notice shall be included in    #
# all copies or substantial portions of the Software.                           #
#                                                                               #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,      #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE   #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER        #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING       #
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER           #
# DEALINGS IN THE SOFTWARE.                                                     #
#                                                                               #
#################################################################################


import os
Import ('env', 'distdir')

libenv = env.Clone ()

if libenv['PLATFORM'] == 'win32':
    libenv.MergeFlags ('-D_DLL')

if not 'wince' in libenv['host']:
   libenv.MergeFlags ('-ansi -pedantic')

# Build the shared library

lib_sources = Split ("""
	    syx-bytecode.c syx-init.c syx-interp.c
	    syx-lexer.c syx-memory.c syx-object.c syx-parser.c
	    syx-primitives.c syx-scheduler.c syx-utils.c syx-error.c
            syx-plugins.c syx-signal.c syx-profile.c
	    """)

if env['static']:
    lib_objects = libenv.StaticObject (lib_sources)
    lib = libenv.StaticLibrary (target='syx', source=lib_objects)
    Default (lib)
    env.SyxInstall (env['libdir'], lib)

if env['shared']:
    lib_objects = libenv.SharedObject (lib_sources)
    lib = libenv.SharedLibrary (target='syx', source=lib_objects)
    Default (lib)

    target = env.SyxInstall (env['libdir'], lib)
    if env['PLATFORM'] == 'posix':
        senv = env.Clone ()
        senv['ENV'] = os.environ
        targetdep = env.Alias ('install', senv.Command ('ldconfig', lib,
                                                        ['@echo -n Updating ld cache...',
                                                         '@ldconfig -n $libdir',
                                                         '@echo " done"']))
        env.Depends (targetdep, target)

target = env.Install (os.path.join (distdir, 'syx'),
                      lib_sources + ['SConscript', 'Makefile.am', 'Makefile.in', 'syx-config.h.in'])
env.Alias ('sdist', target)

# Install includes

includes = Split("""syx.h syx-interp.h syx-config.h syx-bytecode.h syx-enums.h syx-error.h
            syx-lexer.h syx-memory.h syx-object.h syx-init.h
            syx-parser.h syx-platform.h syx-scheduler.h syx-utils.h
            syx-types.h syx-plugins.h syx-signal.h syx-profile.h""")
incdir = os.path.join (env['includedir'], 'syx')
env.Install (incdir, includes)
env.Alias ('install', incdir)

includes.remove('syx-config.h')
target = env.Install (os.path.join (distdir, 'syx'), includes)
env.Alias ('sdist', target)
